package com.bagtrack.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bagtrack.model.Flight;
import com.bagtrack.service.IFlightService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class FlightController {

    private static final Logger logger = LoggerFactory.getLogger(FlightController.class);

    @Autowired
    private IFlightService flightService;

    public void setFlightDAO(IFlightService flightService) {
        this.flightService = flightService;
    }

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/flights", method = RequestMethod.GET, headers = "Accept=application/json")
    public @ResponseBody
    List<Flight> getMyFlights() {

        List<Flight> flights = flightService.getActiveFlights();
        if (flights != null) {
            System.out.println("I just got back [" + flights.size() + "]");
        }
        return flights;
    }

    @RequestMapping(value = "/flights/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
    public @ResponseBody
    Flight getFlightById(@PathVariable String id) {

        Flight flight = flightService.getFlightById(Long.parseLong(id));
        if (flight != null) {
            System.out.println("I just got back [" + flight.getOrigin() + "]");
        }
        return flight;
    }
}
